package com.ruoyi.apis.service.impl;

import com.ruoyi.apis.dto.AdMessageDTO;
import com.ruoyi.apis.dto.ProjectMessageDTO;
import com.ruoyi.apis.entity.AdMessageEntity;
import com.ruoyi.apis.entity.IndustryEntity;
import com.ruoyi.apis.entity.ProjectMessageEntity;
import com.ruoyi.apis.repository.AdMessageRepository;
import com.ruoyi.apis.repository.IndustryRepository;
import com.ruoyi.apis.repository.ProjectMessageRepository;
import com.ruoyi.apis.service.MessageService;
import com.ruoyi.apis.vo.IndustryVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author Choi
 * @Date 2021/8/17
 */

@Service
public class MessageServiceImpl implements MessageService {

    @Autowired
    private IndustryRepository industryRepository;
    @Autowired
    private ProjectMessageRepository projectMessageRepository;
    @Autowired
    private AdMessageRepository adMessageRepository;

    @Override
    public void createAdMessage(AdMessageDTO messageDTO) {
        AdMessageEntity messageEntity = new AdMessageEntity();
        BeanUtils.copyProperties(messageDTO, messageEntity);
        messageEntity.setCreateTime(new Date());
        messageEntity.setUpdateTime(new Date());
        adMessageRepository.save(messageEntity);
    }

    @Override
    public void createProjectMessage(ProjectMessageDTO messageDTO) {
        ProjectMessageEntity messageEntity = new ProjectMessageEntity();
        BeanUtils.copyProperties(messageDTO, messageEntity);
        messageEntity.setCreateTime(new Date());
        messageEntity.setUpdateTime(new Date());
        projectMessageRepository.save(messageEntity);
    }

    @Override
    public IndustryVO getIndustry(long industryId) {
        return new IndustryVO(industryRepository.findById(industryId));
    }

    @Override
    public List<IndustryVO> getIndustries() {
        List<IndustryEntity> industryEntities = industryRepository.findAllByOrderBySortAsc();
        return IndustryVO.getList(industryEntities);
    }
}
